home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / ada / gnat1792.zip / gnat179b / t-adainc / s-reatim.adb < prev    next >
Text File  |  1994-05-19  |  11KB  |  383 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                 GNU ADA RUNTIME LIBRARY (GNARL) COMPONENTS               --
  4. --                                                                          --
  5. --                      S Y S T E M . R E A L _ T I M E                     --
  6. --                                                                          --
  7. --                                  B o d y                                 --
  8. --                                                                          --
  9. --                             $Revision: 1.7 $                             --
  10. --                                                                          --
  11. --           Copyright (c) 1991,1992,1993, FSU, All Rights Reserved         --
  12. --                                                                          --
  13. --  GNARL is free software; you can redistribute it and/or modify it  under --
  14. --  terms  of  the  GNU  Library General Public License as published by the --
  15. --  Free Software Foundation; either version 2,  or  (at  your  option) any --
  16. --  later  version.   GNARL is distributed in the hope that it will be use- --
  17. --  ful, but but WITHOUT ANY WARRANTY; without even the implied warranty of --
  18. --  MERCHANTABILITY  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Gen- --
  19. --  eral Library Public License for more details.  You should have received --
  20. --  a  copy of the GNU Library General Public License along with GNARL; see --
  21. --  file COPYING. If not, write to the Free Software Foundation,  675  Mass --
  22. --  Ave, Cambridge, MA 02139, USA.                                          --
  23. --                                                                          --
  24. ------------------------------------------------------------------------------
  25.  
  26. pragma Checks_On;
  27. --  We require checks on for this body, because we rely on the constraint
  28. --  error to keep System.Real_Time.Time values within representable range.
  29.  
  30. with System.Compiler_Exceptions;
  31. --  Uses, function Current_Exceptions
  32.  
  33. with System.Task_Timer_Service;
  34. --  Uses, object Objects
  35. --        procedure Service_Entries
  36.  
  37. with System.Tasking;
  38.  
  39. with Unchecked_Conversion;
  40.  
  41. package body System.Real_Time is
  42.  
  43.    use Task_Clock;
  44.    --  Using it for arithmetic operations
  45.  
  46.    use Tasking.Protected_Objects;
  47.    use Tasking;
  48.  
  49.    package Timer renames System.Task_Timer_Service.Timer;
  50.  
  51.    function To_Access is new
  52.      Unchecked_Conversion (System.Address, Protection_Access);
  53.  
  54.    -----------
  55.    -- Clock --
  56.    -----------
  57.  
  58.    function Clock return Time is
  59.    begin
  60.       return Time (Task_Clock.Machine_Specifics.Clock);
  61.    end Clock;
  62.  
  63.    ---------
  64.    -- "<" --
  65.    ---------
  66.  
  67.    function "<" (Left, Right : Time) return Boolean is
  68.    begin
  69.       return Task_Clock.Stimespec (Left) < Task_Clock.Stimespec (Right);
  70.    end "<";
  71.  
  72.    function "<" (Left, Right : Time_Span) return Boolean is
  73.    begin
  74.       return Task_Clock.Stimespec (Left) < Task_Clock.Stimespec (Right);
  75.    end "<";
  76.  
  77.    ---------
  78.    -- ">" --
  79.    ---------
  80.  
  81.    function ">" (Left, Right : Time) return Boolean is
  82.    begin
  83.       return Right < Left;
  84.    end ">";
  85.  
  86.    function ">" (Left, Right : Time_Span) return Boolean is
  87.    begin
  88.       return Right < Left;
  89.    end ">";
  90.  
  91.    ----------
  92.    -- "<=" --
  93.    ----------
  94.  
  95.    function "<=" (Left, Right : Time) return Boolean is
  96.    begin
  97.       return not (Left > Right);
  98.    end "<=";
  99.  
  100.    function "<=" (Left, Right : Time_Span) return Boolean is
  101.    begin
  102.       return not (Left > Right);
  103.    end "<=";
  104.  
  105.    ----------
  106.    -- ">=" --
  107.    ----------
  108.  
  109.    function ">=" (Left, Right : Time) return Boolean is
  110.    begin
  111.       return not (Left < Right);
  112.    end ">=";
  113.  
  114.    function ">=" (Left, Right : Time_Span) return Boolean is
  115.    begin
  116.       return not (Left < Right);
  117.    end ">=";
  118.  
  119.    ---------
  120.    -- "+" --
  121.    ---------
  122.  
  123.    --  Note that Constraint_Error may be propagated
  124.  
  125.    function "+" (Left : Time; Right : Time_Span) return Time is
  126.    begin
  127.       return Time (Task_Clock.Stimespec (Left) + Task_Clock.Stimespec (Right));
  128.    end "+";
  129.  
  130.    function "+"  (Left : Time_Span; Right : Time) return Time is
  131.    begin
  132.       return Right + Left;
  133.    end "+";
  134.  
  135.    function "+"  (Left, Right : Time_Span) return Time_Span is
  136.    begin
  137.       return Time_Span (Time (Right) + Left);
  138.    end "+";
  139.  
  140.    ---------
  141.    -- "-" --
  142.    ---------
  143.  
  144.    --  Note that Constraint_Error may be propagated
  145.  
  146.    function "-"  (Left : Time; Right : Time_Span) return Time is
  147.    begin
  148.       return Time (Task_Clock.Stimespec (Left) - Task_Clock.Stimespec (Right));
  149.    end "-";
  150.  
  151.    function "-"  (Left, Right : Time) return Time_Span is
  152.    begin
  153.       return Time_Span (Left - Time_Span (Right));
  154.    end "-";
  155.  
  156.    function "-"  (Left, Right : Time_Span) return Time_Span is
  157.    begin
  158.       return Time_Span (Time (Left) - Right);
  159.    end "-";
  160.  
  161.    function "-"  (Right : Time_Span) return Time_Span is
  162.    begin
  163.       return Time_Span (-(Task_Clock.Stimespec (Right)));
  164.    end "-";
  165.  
  166.    ---------
  167.    -- "/" --
  168.    ---------
  169.  
  170.    --  Note that Constraint_Error may be propagated
  171.  
  172.    function "/"  (Left, Right : Time_Span) return integer is
  173.       Temp_TV : Task_Clock.Stimespec;
  174.  
  175.    begin
  176.       Temp_TV := Task_Clock.Stimespec (Left) / Task_Clock.Stimespec (Right);
  177.  
  178.       if not (Temp_TV < Task_Clock.Stimespec_Zero)  then
  179.          return Task_Clock.Stimespec_Seconds (Temp_TV);
  180.       end if;
  181.  
  182.       Temp_TV := -Temp_TV;
  183.       return -Task_Clock.Stimespec_Seconds (Temp_TV);
  184.    end "/";
  185.  
  186.    function "/"  (Left : Time_Span; Right : Integer) return Time_Span is
  187.    begin
  188.       return Time_Span (Task_Clock.Stimespec (Left) / Right);
  189.    end "/";
  190.  
  191.    ---------
  192.    -- "*" --
  193.    ---------
  194.  
  195.    --  Note that Constraint_Error may be propagated
  196.  
  197.    function "*"  (Left : Time_Span; Right : Integer) return Time_Span is
  198.    begin
  199.       return Time_Span (Task_Clock.Stimespec (Left) * Right);
  200.    end "*";
  201.  
  202.    function "*"  (Left : Integer; Right : Time_Span) return Time_Span is
  203.    begin
  204.       return Right * Left;
  205.    end "*";
  206.  
  207.    -----------
  208.    -- "abs" --
  209.    -----------
  210.  
  211.    --  Note that Constraint_Error may be propagated
  212.  
  213.    function "abs" (Right : Time_Span) return Time_Span is
  214.    begin
  215.       if Right < Time_Span_Zero then
  216.          return -Right;
  217.       end if;
  218.  
  219.       return Right;
  220.    end "abs";
  221.  
  222.    -----------------
  223.    -- To_Duration --
  224.    -----------------
  225.  
  226.    function To_Duration (FD : Time_Span) return Duration is
  227.    begin
  228.       return Task_Clock.Stimespec_To_Duration (Task_Clock.Stimespec (FD));
  229.    end To_Duration;
  230.  
  231.    ------------------
  232.    -- To_Time_Span --
  233.    ------------------
  234.  
  235.    function To_Time_Span (D : Duration) return Time_Span is
  236.    begin
  237.       return Time_Span (Task_Clock.Duration_To_Stimespec (D));
  238.    end To_Time_Span;
  239.  
  240.    -----------------
  241.    -- Nanoseconds --
  242.    -----------------
  243.  
  244.    function Nanoseconds (NS : integer) return Time_Span is
  245.    begin
  246.       return Time_Span_Unit * NS;
  247.    end Nanoseconds;
  248.  
  249.    ------------------
  250.    -- Microseconds --
  251.    ------------------
  252.  
  253.    function Microseconds  (US : integer) return Time_Span is
  254.    begin
  255.       return Nanoseconds (US) * 1000;
  256.    end Microseconds;
  257.  
  258.    -------------------
  259.    --  Milliseconds --
  260.    -------------------
  261.  
  262.    function Milliseconds (MS : integer) return Time_Span is
  263.    begin
  264.       return Microseconds (MS) * 1000;
  265.    end Milliseconds;
  266.  
  267.    ------------------
  268.    -- Delay_Object --
  269.    ------------------
  270.  
  271.    --  Hand translated code will be provided here
  272.    --  until the GNAT compiler can accomodate the protected objects. ???
  273.  
  274.    package body Delay_Object is
  275.  
  276.       procedure Service_Entries (Pending_Serviced : out Boolean) is
  277.          P : System.Address;
  278.  
  279.          subtype PO_Entry_Index is Protected_Entry_Index
  280.            range Null_Protected_Entry .. 1;
  281.  
  282.          Barriers : Barrier_Vector (1 .. 1)  :=  (others => true);
  283.          --  No barriers. always true barrier
  284.  
  285.          E : PO_Entry_Index;
  286.  
  287.          PS : Boolean;
  288.  
  289.          Cumulative_PS : Boolean := False;
  290.  
  291.       begin
  292.          loop
  293.             --  Get the next queued entry or the pending call
  294.             --  (if no barriers are true)
  295.  
  296.             Next_Entry_Call (To_Access